package com.company.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @Auther  James_Gosling
 * @Date  2021-4-14
 * @Version 1.0
 * @Desc 计算工具类，运算并保留小数
 */

public class MathTool {
    //自动去0，返回double,加
    public  Integer add(Integer ...num){
        Integer sum = 0;
        for (Integer integer : num) {
            sum+=integer;
        }
        return sum;
    }
    public  Double add(Double ...num){
        Double sum = 0d;
        for (Double integer : num) {
            sum+=integer;
        }
        return sum;
    }
    //自动去0，返回double,减
    public static double minus(double d1, double d2, int scale){
        BigDecimal big = new BigDecimal(d1-d2);
        System.out.println(big);
        return  big.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    //自动去0，返回double,乘
    public static double  multiply(double d1, double d2, int scale){
        BigDecimal big = new BigDecimal(d1*d2);
        return  big.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    //自动去0，返回double,除
    public static double divide(double d1, double d2, int scale){
        BigDecimal big = new BigDecimal(d1/d2);
        System.out.println(big);
        return  big.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    //返回String,加
    public static String addString(double d1, double d2, int scale){
        BigDecimal big = new BigDecimal(String.valueOf(d1+d2));
        StringBuilder format = new StringBuilder("0.");
        for(int i=0;i<scale;i++){
            format.append(0);
        }
        DecimalFormat df = new DecimalFormat(format.toString());
        return  df.format(big.setScale(scale,BigDecimal.ROUND_HALF_UP));
    }
    //返回String,减
    public static String minusString(double d1, double d2, int scale){
        BigDecimal big = new BigDecimal(String.valueOf(d1-d2));
        StringBuilder format = new StringBuilder("0.");
        for(int i=0;i<scale;i++){
            format.append(0);
        }
        DecimalFormat df = new DecimalFormat(format.toString());
        return  df.format(big.setScale(scale,BigDecimal.ROUND_HALF_UP));
    }
    //返回String,乘
    public static String  multiplyString(double d1, double d2, int scale){
        BigDecimal big = new BigDecimal(String.valueOf(d1*d2));
        StringBuilder format = new StringBuilder("0.");
        for(int i=0;i<scale;i++){
            format.append(0);
        }
        DecimalFormat df = new DecimalFormat(format.toString());
        return  df.format(big.setScale(scale,BigDecimal.ROUND_HALF_UP));
    }
    //返回String,除
    public static String divideString(double d1, double d2, int scale){
        BigDecimal big = new BigDecimal(String.valueOf(d1/d2));
        StringBuilder format = new StringBuilder("0.");
        for(int i=0;i<scale;i++){
            format.append(0);
        }
        DecimalFormat df = new DecimalFormat(format.toString());
        return  df.format(big.setScale(scale,BigDecimal.ROUND_HALF_UP));
    }
    public static Double formateToString(double d) {
        /**patten
         *0  前面的值没有会补0
         *# 0会被缺省
         * ,
         * %
         */
        String pattern = "00000";
        DecimalFormat dec = new DecimalFormat(pattern);
//        System.out.println(dec.format(d));
        return  Double.parseDouble(dec.format(d));
    }
    public static int getMax(int[] list){
        int max=list[0];
        for(int i=0;i<list.length;i++){
            max = list[i]>max? list[i]:max;
        }
        return max;
    }
    public static float getMax(float[] list){
        float max=list[0];
        for(int i=0;i<list.length;i++){
            max = list[i]>max? list[i]:max;
        }
        return max;
    }
    public static double getMax(double[] list){
        double max=list[0];
        for(int i=0;i<list.length;i++){
            max = list[i]>max? list[i]:max;
        }
        return max;
    }
    public static long getMax(long[] list){
        long max=list[0];
        for(int i=0;i<list.length;i++){
            max = list[i]>max? list[i]:max;
        }
        return max;
    }
}
